package com.bfly.mall.message.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.common.util.DataConvertUtils;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.core.security.ActionModel;
import com.bfly.mall.message.entity.Notify;
import com.bfly.mall.message.dto.NotifyFormDTO;
import com.bfly.mall.message.dto.NotifyListDTO;
import com.bfly.mall.message.service.INotifyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统通知Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/11/1 10:47
 */
@RestController
@RequestMapping(value = "/manage/notify")
public class NotifyController extends BaseController {

    @Autowired
    private INotifyService notifyService;

    /**
     * 系统通知列表
     * @author 长沙商脉科技有限公司
     * @date 2020/11/1 10:49
     */
    @GetMapping(value = "/list")
    public void listNotify(HttpServletResponse response, HttpServletRequest request) {
        PagerThreadLocal.set(request);

        Map<String, Object> exact = new HashMap<>(1);
        Map<String, String> unExact = new HashMap<>(3);
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String revoke = request.getParameter("revoke");
        if (StringUtils.isNotBlank(title)) {
            unExact.put("title", title);
        }
        if (StringUtils.isNotBlank(content)) {
            unExact.put("content", content);
        }
        if (StringUtils.isNotBlank(revoke)) {
            exact.put("is_revoke", DataConvertUtils.convertToBoolean(revoke));
        }

        Pager<Notify> pager = notifyService.getPage(exact,unExact,null);
        Pager<NotifyListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());

        if (pager.getData() != null) {
            List<NotifyListDTO> list = new ArrayList<>();
            for (Notify notify : pager.getData()) {
                NotifyListDTO dto = new NotifyListDTO();
                BeanUtils.copyProperties(notify, dto);
                list.add(dto);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 发布系统通知
     * @author 长沙商脉科技有限公司
     * @date 2020/11/1 10:49
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "发布系统通知")
    public void addNotify(HttpServletResponse response, @Valid @RequestBody NotifyFormDTO formDTO, BindingResult result) {
        validData(result);
        notifyService.save(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 编辑系统通知
     * @author 长沙商脉科技有限公司
     * @date 2020/11/1 10:52
     */
    @PostMapping(value = "/edit")
    @ActionModel(value = "编辑系统通知")
    public void editNotify(HttpServletResponse response, @Valid @RequestBody NotifyFormDTO formDTO, BindingResult result) {
        validData(result);
        notifyService.edit(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 编辑系统通知撤销状态
     * @author 长沙商脉科技有限公司
     * @date 2020/11/1 10:51
     */
    @GetMapping(value = "/revoke")
    @ActionModel(value = "编辑系统通知撤销状态")
    public void editNotifyRevoke(HttpServletResponse response, @RequestParam("notifyId") int notifyId, @RequestParam("revoke") boolean revoke) {
        notifyService.editNotifyRevoke(notifyId, revoke);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除系统通知
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/3 21:56
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除系统通知")
    public void delNotify(HttpServletResponse response, @RequestBody Integer... ids) {
        notifyService.remove(ids);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}